#ifndef JPEGCODEC_H
#define JPEGCODEC_H

#include <inttypes.h>

#ifndef uchar
typedef unsigned char uchar;
#endif

#ifndef uint
typedef unsigned int uint;
#endif

#ifndef ulong
typedef unsigned long ulong;
#endif

class JpegCodec
{
    uchar*  _planes[3];
    uint*
    _quantMatrix;
    uint    _bufferLines;
    uint    _bufferLineWidth;

    // RGB -> YCbCr
    uint8_t Y(uint8_t R, uint8_t G, uint8_t B);
    uint8_t Cb(uint8_t R, uint8_t G, uint8_t B);
    uint8_t Cr(uint8_t R, uint8_t G, uint8_t B);

    // Discrete cosine transform at 8x8 block. Block pixels will be replaced with (signed) DCT coefficients
    void DCT(uchar *block);

    // Quantize 8x8 block to kill most of its data :'(
    void quantize(uchar *block);

public:
    JpegCodec();
    ~JpegCodec();

    // encode image from RGB24 format to JPEG's 8x8 quantized DCT
    bool encodeJpeg(const uchar* srcBuffer, uchar* destBuffer, uint lines, uint lineWidth);
};

#endif // JPEGCODEC_H
